/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.Material;
import org.crystalrange.enterprise.model.Plant;
import org.crystalrange.enterprise.model.Quant;
import org.crystalrange.enterprise.model.StorageBin;
import org.crystalrange.enterprise.model.StorageType;
import org.crystalrange.enterprise.model.Unit;
import org.crystalrange.enterprise.model.WarehouseNumber;
import org.crystalrange.enterprise.service.QuantService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Window;

public class QuantController extends BaseController {

    protected QuantService quantService;
    protected Quant quant;

    public QuantService getQuantService() {
        return this.quantService;
    }

    public void setQuantService(QuantService quantService) {
        this.quantService = quantService;
    }

    public Quant getQuant() {
        return this.quant;
    }

    public void setQuant(Quant quant) {
        this.quant = quant;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.quant = this.quantService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Quant: " + this.quant.getId());
        } else {
            this.quant = this.quantService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Quant");
        }

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemQuantSave(Event event) throws Exception {
        this.quantSave();
    }

    public void onClick$toolbarbuttonQuantSave(Event event) throws Exception {
        this.quantSave();
    }

    public void onOpen$bandboxPlant(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/plantSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Plant selectedPlant = (Plant) this.execution.getAttribute("selectedPlant");
            this.quant.setPlant(selectedPlant);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxWarehouseNumber(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/warehouseNumberSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            WarehouseNumber selectedWarehouseNumber = (WarehouseNumber) this.execution.getAttribute("selectedWarehouseNumber");
            this.quant.setWarehouseNumber(selectedWarehouseNumber);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxStorageType(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/storageTypeSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            StorageType selectedStorageType = (StorageType) this.execution.getAttribute("selectedStorageType");
            this.quant.setStorageType(selectedStorageType);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxStorageBin(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/storageBinSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            StorageBin selectedStorageBin = (StorageBin) this.execution.getAttribute("selectedStorageBin");
            this.quant.setStorageBin(selectedStorageBin);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxMaterial(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/materialSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Material selectedMaterial = (Material) this.execution.getAttribute("selectedMaterial");
            this.quant.setMaterial(selectedMaterial);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxTotalStockUnit(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/unitSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Unit selectedUnit = (Unit) this.execution.getAttribute("selectedUnit");
            this.quant.setTotalStockUnit(selectedUnit);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxWeightUnit(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/unitSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Unit selectedUnit = (Unit) this.execution.getAttribute("selectedUnit");
            this.quant.setWeightUnit(selectedUnit);
        }

        this.binder.loadAll();
    }

    private void quantSave() throws Exception {
        this.quantService.save(this.quant,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
